<?php 
session_start();
include_once('functions.inc.php');
//-------

$s = new Session();

// Session doesn't exist
if(!$s->chkSession() and isset($_POST['login']) and isset($_POST['pwd']))
{
	$login = $_POST['login'];
	$pwd = $_POST['pwd'];
	if(!$s->loadSession($login,$pwd))
	{
		// Reset cookies, so logging will be possible next time
		setcookie("session","",time()-3600);
		setcookie("user","",time()-3600);
		// Stop software
		die('Authentication failed');
	}
	else
		header('location:index.php');
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<title>Valhalla::NOTES</title>
	<link href='http://fonts.googleapis.com/css?family=Ubuntu' rel='stylesheet' type='text/css'>
	<link rel="stylesheet" href="styles/styles.css">
	<?php if($s->chkSession()): ?>
	<script src="scripts/jquery-1.4.2.min.js" type="text/javascript" charset="utf-8"></script>
	<script src="scripts/main.js" type="text/javascript" charset="utf-8"></script>
	<?php endif ?>
</head>
<body>
<div id="header"><?php include_once('views/header.php'); ?></div>
<div id="container">
	<?php if($s->chkSession()): ?>
	<div id="tools"><?php include_once('views/tools.php'); ?></div>
	<div id="main">
		<div id="sidebar" style="display:none;"><?php include_once('views/sidebar.php'); ?></div>
		<div id="page"></div>
	</div>
	<?php else: ?>
	<div id="main">
		<form id="login_form" action="index.php" method="post" accept-charset="utf-8">
			<p><input type="text" name="login" value="" id="lf_login"></p>
			<p><input type="password" name="pwd" value="" id="lf_pwd"></p>
			<p><input type="submit" value="Log In &rarr;"></p>
		</form>
	</div>
	<?php endif; ?>
</div>
<div id="footer"><?php include_once('views/footer.php'); ?></div>
</body>
</html>